package com.ican.study2.common.util;

import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import com.ican.study2.common.common.BusinessException;

public class XmlUtils {
		
	/**
	 * 加载xml文件并转换成dom
	 * @param path  xml文件目录
	 * @return Map  把生成的dom放入map，其键是文件名
	 * @throws BusinessException 
	 */
	public static Map<String, Document> loadXmlAnd2Dom(String path) throws BusinessException {
		Map<String, Document> map = null;//加裁xml文件
		File file = new File(path);
		if (file.exists()){
			map = new HashMap<String, Document>();
			if (file.isFile()) {
				map.put(file.getName(), xml2Doc(file));
			} else if(file.isDirectory()){
				File[] files = file.listFiles();
				for (int i = 0; i < files.length; i++) {
					loadXmlAnd2DomAcc(files[i], map);
				}
			}
		}
		return map;
	}
	
	/**
	 * 加载xml文件并转换成dom
	 * @param file  xml文件
	 * @return Map  把生成的dom放入map，其键是文件名
	 * @throws BusinessException 
	 */
	public static Map<String, Document> loadXmlAnd2Dom(File file) throws BusinessException {
		Map<String, Document> map = null;//加裁xml文件
		if (file.exists()){
			map = new HashMap<String, Document>();
			if (file.isFile()) {
				map.put(file.getName(), xml2Doc(file));
			} else if(file.isDirectory()){
				File[] files = file.listFiles();
				for (int i = 0; i < files.length; i++) {
					loadXmlAnd2DomAcc(files[i], map);
				}
			}
		}
		return map;
	}
	
	public static void loadXmlAnd2DomAcc(File file, Map<String, Document> map) throws BusinessException {
		if (file.isFile()) {
			map.put(file.getName(), xml2Doc(file));
		} else if(file.isDirectory()){
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				loadXmlAnd2DomAcc(files[i], map);
			}
		}
	}
	
	public static Document xml2Doc(File file) throws BusinessException{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			return db.parse(file);
		} catch (Exception e) {
			throw new BusinessException("文件" + file.getName() + "解析成DOM时出错！", e);
		}
	}
	
	public static Document xml2Doc(InputStream in) throws BusinessException{
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            return db.parse(in);
        } catch (Exception e) {
            throw new BusinessException("解析成DOM时出错！", e);
        }
    }
	
	/**
	 * xml文件内容转换成dom
	 * @param content  xml文件内容
	 * @return Map  把生成的dom放入map，其键是文件名
	 * @throws BusinessException 
	 */
	public static Document xmlContent2Dom(String content) throws BusinessException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		StringReader sr = new StringReader(content);
		InputSource is = new InputSource(sr);
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			return db.parse(is);
		} catch (Exception e) {
			throw new BusinessException("文件内容解析成DOM时出错！", e);
		}
	}
	
	/**
	 * 把dom写入文件
	 * @param doc  doc对像
	 * @param path 要写入的文件夹
	 * @param fileName 要写入的文件名
	 * @return boolean  保存是否成功
	 * @throws BusinessException 
	 * @throws FileNotFoundException 
	 */
	public static boolean doc2File(Document doc, String path, String fileName) throws BusinessException{
		File file = null;
		Transformer trans = null;
		try {
			file = new File(path + fileName);
			trans = TransformerFactory.newInstance().newTransformer();
			trans.setOutputProperty("encoding", "utf-8");
			DOMSource ds = new DOMSource(doc);
			StreamResult sr = new StreamResult(file);
			trans.transform(ds, sr);
		} catch (Exception e) {
			throw new BusinessException("把DOM内容写入文件" + path + fileName + "时出错！", e);			
		}
		return true;
	}
}